const app = getApp()
var CONFIG = require("../../config/config.js");
var share = require('../../utils/share.js');
 

Page({
  data: {
      identity:1,
    is_admin: 0,
    uc_id:'',
    list: [],
    course:[],
    picKan:false,
    picindex:0,
    jietu:"",
    canvasHidden:false,
    showerca:false,
    pay_methods: [],
    imgback: '/images/qikeya1.png',
    imgsrc:'',
    showorder:false,
    dellist:[],
    ucp_id:'',  //取消的订单id
    showfalse:false,
    remarkcli:'',
    closefalse:false,
    hktext:'',
    kh:'',   //1退款  2换课 3更换折扣
    chentext:'',
    closefalse1:false,
    codeimg:'',   //二维码图片
    havebuk:0,
    indicatorDots: true,
    interval: 5000,
    duration: 1000,
    imgUrl:CONFIG.API_PIC,
    audit_id:0,  //申请表id
      newconid:'',
  }, 
   //    点击取消
    chenclose:function(){
        this.setData({
           closefalse:false
        })
    },
    


    closetkno(){
        wx.showToast({
            title: '充值卡类型的课不能进行退费!',
            icon: 'none',
            duration: 3000,
        })
    },
    moneyclickno(){
        wx.showToast({
            title: '充值卡类型的课不能进行换课!',
            icon: 'none',
            duration: 3000,
        })
    },
    
    // 点击可撤销的退款
    closetk(){
        var that = this;
        that.setData({
            hktext:'您确定要撤销该课程的退款申请吗',
            closefalse: true,
            kh:1,
        })
    },
    // 点击不可撤销的退款
    closetk1(){
        wx.showToast({
            title: '该课程暂不能撤销退款',
            icon: 'none',
            duration: 3000,
        })
    },


    // 点击可撤销的换课
    closehk() {
        var that = this;
        that.setData({
            hktext: '您确定要撤销该课程的换课申请吗',
            closefalse: true,
            kh: 2,
        })
    },
    // 点击不可撤销的换课
    closehk1() {
        wx.showToast({
            title: '该课程暂不能撤销换课',
            icon: 'none',
            duration: 3000,
        })
    },

    // 点击可撤销的更换折扣
    closezk() {
        var that = this;
        that.setData({
            hktext: '您确定要撤销该课程的更换折扣申请吗',
            closefalse: true,
            kh: 3,
        })
    },
    // 点击不可撤销的换课
    closezk1() {
        wx.showToast({
            title: '该课程暂不能撤销更换折扣',
            icon: 'none',
            duration: 3000,
        })
    },
 

    // 点击撤销
    hkclose(){
        wx.showLoading({
            title: '撤销中~',
        })
        var that = this;
        var kh = that.data.kh;   //1退款  2换课
        that.refundReject(kh);
    },

    closeque(){
        var that = this;
        that.setData({
            closefalse1: false
        })
        that.payLogsInit(that.data.uc_id, 2)
    },

    // 退款申请 - 撤销
    refundReject(kh){
        var that = this;
        if (kh == 1 || kh == '1'){
            var type = 1;
        }else if (Number(kh) == 3){
            var type = 3;
        }else{
            var type = 2;
        }
        share.wxRequest('crmapi/user/changeRefundReject',{
            audit_id: that.data.audit_id,
            type:type,
            is_type:2
        },function(res){
            wx.hideLoading();
            if (Number(res.data.errcode) == 0){
                if (Number(kh) == 1){
                    that.setData({
                        chentext:'退费申请已成功撤销'
                    })
                }else if (Number(kh) == 3){
                    that.setData({
                        chentext:'更换折扣申请已成功撤销'
                    })
                }else{
                    that.setData({
                        chentext: '换课申请已成功撤销'
                    })
                }
                that.setData({
                    closefalse1:true,
                    closefalse: false
                })
            }else{
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 3000,
                })
            }
        })
    },
    


    /// 长按
    longTap: function (e) {
        // console.log("long tap")
        var codeimg = this.data.codeimg;
        share.sendImage(codeimg);
    },

    // 查看备注
    clearclick1(e){
       var that = this;
       var remark = e.currentTarget.dataset.remark;
        that.setData({
            remarkcli: remark,
            showfalse1:true
        })
    },

    sureclick1(){
        var that = this;
        that.setData({
            showfalse1: false
        })
    },


    nopayclick(){
        var that = this;
        var course = that.data.course;
        var status = course.status;
        // < !--status：状态(0未结清 1已结清 2退款中 3换课中 4已退费 5已换课)-- >
        if (Number(status) == 3) {
            wx.showToast({
                title: '该课程正在换课，不能补款',
                icon: 'none',
                duration: 3000,
            })
        } else if (Number(status) == 2) {
            wx.showToast({
                title: '该课程正在退费，不能补款',
                icon: 'none',
                duration: 3000,
            })
        }else if (Number(status) == 7) {
            wx.showToast({
                title: '该课程正在申请更换折扣，不能退费！',
                icon: 'none',
                duration: 3000,
            })
        }else{
            wx.showToast({
                title: '该课程已结清',
                icon: 'none',
                duration: 3000,
            })
        }
       
    },
    nopayclick1(){
        wx.showToast({
            title: '该课程有待确认或者未支付订单，暂不能补款',
            icon: 'none',
            duration: 3000,
        })
    },

    moneyclick(){
        var that = this;
        var course = that.data.course;
        var status = course.status;
        // is_exchange 是否在换课中 0: 没换课或者换课完成   1: 换课中 默认0  
        if (Number(status) == 2) {
            wx.showToast({
                title: '该课程正在退款，不能换课！',
                icon: 'none',
                duration: 3000,
            })
        }else if (Number(status) == 7) {
            wx.showToast({
                title: '该课程正在申请更换折扣，不能退费！',
                icon: 'none',
                duration: 3000,
            })
        }else{
            wx.showToast({
                title: '该课程有付款未审核或有未付款订单，不能换课！',
                icon: 'none',
                duration: 3000,
            })
        }
        
    },

    decourseclick() {
        var that = this;
        var course = that.data.course;
        var status = course.status;
        // is_exchange 是否在换课中 0: 没换课或者换课完成   1: 换课中 默认0
        if (Number(status) == 3) {
            wx.showToast({
                title: '该课程正在换课，不能退费！',
                icon: 'none',
                duration: 3000,
            })
        }else if (Number(status) == 7) {
            wx.showToast({
                title: '该课程正在申请更换折扣，不能退费！',
                icon: 'none',
                duration: 3000,
            })
        }  else {
            wx.showToast({
                title: '该课程有付款未审核或有未付款订单，不能退费！',
                icon: 'none',
                duration: 3000,
            })
        }
        
    },
    zkchangeclick(){
        var that = this;
        var course = that.data.course;
        var status = course.status;
        // is_exchange 是否在换课中 0: 没换课或者换课完成   1: 换课中 默认0  
        if (Number(status) == 2) {
            wx.showToast({
                title: '该课程正在退款，不能更换折扣！',
                icon: 'none',
                duration: 3000,
            })
        }else if (Number(status) == 3) {
            wx.showToast({
                title: '该课程正在换课，不能更换折扣！',
                icon: 'none',
                duration: 3000,
            })
        }else{
            wx.showToast({
                title: '该课程有付款未审核或有未付款订单，不能更换折扣！',
                icon: 'none',
                duration: 3000,
            })
        }
    },

  /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function (options) {
        // console.log(options);
        var uc_id = options.uc_id;
        var consultant_id = app.globalData.admin_id;
        wx.setStorageSync('followtype',3)
        if(wx.getStorageSync('audit_id')){
            var audit_id = wx.getStorageSync('audit_id');
            wx.removeStorageSync('audit_id')
        }else{
            var audit_id = options.audit_id
        }
        if (options.newconid){
            var newconid = options.newconid;
        }else{
            var newconid = '';
        }
        // console.log(audit_id);
        this.setData({
            uc_id: uc_id,
            // uc_id: 62,
            is_admin: consultant_id,
            audit_id:audit_id,
            newconid: newconid,
        });
        
        this.payLogsInit(uc_id,2);
        // this.getPayMethods();
    },
    onShow(){
        var consultant_id = app.globalData.admin_id;
        this.setData({
            // is_admin: 1,
            is_admin: consultant_id,
        });
        if(wx.getStorageSync('audit_id')){
            var audit_id = wx.getStorageSync('audit_id');
            wx.removeStorageSync('audit_id')
            this.setData({
                audit_id:audit_id
            });
        }else{
        }
        this.payLogsInit(this.data.uc_id,2);
        // this.getPayMethods();
    },
    

    // 选择您要取消的订单
    radioChange(e){
        var that = this;
        // console.log(e);
        that.setData({
            ucp_id: e.detail.value,
        })
    },
    
    // 点击确定
    yesclick(){
        var that = this;
        var ucp_id = that.data.ucp_id
        if (ucp_id==''){
            wx.showToast({
                title: '请选择您要取消的订单',
                icon: 'none'
            })
        }else{
            wx.showLoading({
                title: '加载中~',
                mask: true
            })
            // console.log(ucp_id);
            that.orderUndo(ucp_id);
        }
    },

    orderUndo(ucp_id){
        var that = this;
        var obj = {
            ucp_id: ucp_id
        }
        if (Number(app.globalData.identity) == 3) {
            var url = 'crmapi/Learn/orderUndo';
        }else{
            var url = 'crmapi/user/orderUndo';
        }
        share.wxRequest(url,obj,function(res){
            wx.hideLoading();
            wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration:2000,
            })
            // console.log(res);
            if (Number(res.data.errcode) == 0) {
                that.setData({
                    showorder: false
                })
                that.payLogsInit(that.data.uc_id,1);
            }
        })
    },

    // 点击取消
    noclick(){
        var that = this;
        that.setData({
            showorder: false
        })
    },

    // 点击取消订单
    detalclick(){
        var that = this;
        var dellist = that.data.dellist;
        if (dellist.length == 0 || dellist.length == '0'){
            wx.showToast({
                title: '没有可以取消的订单',
                icon: 'none'
            })
        }else{
            that.setData({
                showorder:true
            })
        }
    },




    closeImage() {
        var that = this;
        that.setData({
            showerca: false
        })
    },

    // 点击查看凭证
    clearclick:function(e){
    // console.log(e);
        wx.showLoading({
          title: '加载中~',
          mask: true
        })
        var that = this;
        var ototype = e.target.dataset.ototype;
        var index = e.target.dataset.index;
        var list = that.data.list;
        // console.log(ototype)
        that.setData({
            picindex: index
        })
        if (Number(ototype) == 1){   //线下
            var newjietu = list[index].jietu;
            var srr = newjietu.split(',');
            // console.log(srr);
            wx.hideLoading();
            if (Number(srr.length)>0) {
                that.setData({
                  picKan: true,
                  jietu: srr
                })
            } else {
                wx.showToast({
                  title: '暂无凭证',
                  icon: "none",
                  duration: 1500
                })
            }
      }else{
            // var pay_methods = that.data.pay_methods;
            var paytitle = '';
            var course = that.data.course;
            var id = list[index].pay_method;
            var amount = list[index].amount;
            var imgback = that.data.imgback;
            var jiaoyanCode = CONFIG.API_PIC + list[index].qr_img;
            // var jiaoyanCode = list[index].qr_code;
            that.setData({
                codeimg: jiaoyanCode,
                showerca: true
            })
            wx.hideLoading();
            
      }
    
    },

    // 保存图片到相册
    saveImagephone() {
        share.saveImage('canvas');
    },


    sendImagephone(){
        share.sendImage('canvas');
    },


  //点击查看凭证关闭
  closeclick:function(){
    this.setData({
      picKan:false,
      jietu:''
    })
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.payLogsInit(this.data.uc_id,2);
    wx.stopPullDownRefresh();
  },

  payLogsInit: function (uc_id,type) {
    wx.showLoading({
      title: '加载中~',
      mask: true
    })
    let that = this;
      var identity = app.globalData.identity;
      that.setData({
          identity: identity
      })
        if (Number(identity)==3){
            if (that.data.newconid != '' && Number(that.data.newconid)>0){
                var url = 'crmapi/user/userCoursePay'
            }else{
                var url = 'crmapi/Learn/userCoursePay'
            }
        }else{
            var url = 'crmapi/user/userCoursePay'
        }
      share.wxRequest(url,{uc_id:uc_id},function(res){
        if (Number(res.data.errcode) == 0){
            var data = res.data.data;
            // if (Number(identity) == 1) {
            //     var list = data;
            // }else{
                var list = data.list;
            // }
            var dellist = [];   
            //   is_undo   0 不可以取消订单  1可以取消订单 
            //   oto_type  1线下  2线上
            // ucp_status  0 线下未确认   1线下已确认
            // pay_status  2 线上待支付   1线上已确认
            var havebuk = 0;
            if (Number(identity) != 1) {
                if(Number(list.length)>=1){
                    var evitem = list[0];
                    // console.log(evitem)
                    // "pay_status": 1,//支付状态：1：支付完成 2：未付款（选择线上支付，刚生成的订单是未付款的状态）
                    var pay_status = evitem.pay_status;
                    // "ucp_status": 0,//状态(0,待确认；1、已确认，2、已退款 3:已拒绝)
                    var ucp_status = evitem.ucp_status;
                    // "oto_type": 1,// 1:线下支付 2:线上支付
                    var oto_type = evitem.oto_type;
                    if(Number(oto_type)==1){
                        if(Number(ucp_status)<=0||Number(ucp_status)==3){
                            var havebuk = 1
                        }
                    }else{
                        if (Number(pay_status) == 2 || Number(pay_status) == 3){
                            var havebuk = 1
                        }
                    }
                }
                for (var i = 0; i < list.length; i++) {
                    if (Number(list[i].is_undo) == 1) {
                        if (Number(list[i].oto_type) == 1){
                            if (Number(list[i].ucp_status) == 0) {
                                dellist.push(list[i]);
                            }
                        }else{
                            if (Number(list[i].pay_status) == 2) {
                                dellist.push(list[i]);
                            }
                        }
                        
                    }
                }
            }
            that.setData({
                course: data.course,
                list: list,
                dellist: dellist,
                havebuk:havebuk
            })
            // console.log(that.data.dellist)
            wx.hideLoading();
        } else if (Number(res.data.errcode) == 1002) {
            wx.hideLoading();
            if (Number(type) == 1){
                that.setData({
                    showfalse:true,
                })
            }
        }
    })
  },
  
    sureclick(){
        this.setData({
            showfalse: false,
        })
        wx.setStorageSync('followtype',3);
        wx.navigateBack();
    },

  // 用户点击右上角分享

  onShareAppMessage: function () {
    return {
      title: CONFIG.SHARETITLE,
      path: CONFIG.PATHLOGIN
    }
  }
})